package controllers

import (
	"blog_api/models"
	"blog_api/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/keemi-zhou/validation"
	"strconv"
	"strings"
	"time"
)

type ArticleController struct {
	BaseController
}

/**
 * IndexHotArticleList
 * @Description: 获取推荐列表
 * @Author: Jazk-Z
 * @receiver a
 * @param c
 */
func (a ArticleController) IndexHotArticleList(c *gin.Context) {
	limit := c.DefaultQuery("limit", "7")
	res := models.HotArticleList(limit)
	a.success(c, "success", res)
	return
}

/**
 * IndexArticleListByCat
 * @Description: 首页文章列表
 * @Author: Jazk-Z
 * @receiver a
 * @param c
 */
func (a ArticleController) IndexArticleListByCat(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", "6"))
	cat_id, _ := strconv.Atoi(c.DefaultQuery("cid", "0"))
	lid, _ := strconv.Atoi(c.DefaultQuery("lid", "0"))
	keyword := c.DefaultQuery("keyword", "")
	offset := (page - 1) * limit

	list, total := models.ArticleListByCat(limit, offset, cat_id, lid, keyword, 0)
	data := make(map[string]interface{})
	data["data"] = list
	data["total"] = total
	data["url"] = "http://" + c.Request.Host + "/static"
	a.success(c, "success", data)
	return
}

/**
 * IndexArticleClick
 * @Description: 点击文章去详情页
 * @Author: Jazk-Z
 * @receiver a
 * @param c
 */
func (a ArticleController) IndexArticleClick(c *gin.Context) {
	art_id := c.Query("art_id")
	ip := strings.TrimSpace(c.DefaultQuery("ip", ""))
	location := strings.TrimSpace(c.DefaultQuery("location", ""))

	valid := validation.Validation{}
	valid.Required(art_id, "文章id丢了")
	if valid.HasErrors() {
		for _, err := range valid.Errors {
			a.error(c, err.Key, "")
			return
		}
	}

	ip2int, _ := utils.Ip2Long(ip)

	var pvl = models.PageVisitLog{
		Ip:         ip,
		Ip2int:     ip2int,
		Location:   location,
		Client:     c.Request.UserAgent(),
		CreateTime: time.Now().Unix(),
		VisitTimes: 1,
		Page:       fmt.Sprintf("/Details?id=%v", art_id),
		Log:        fmt.Sprintf("%vid为：%v", models.CODE2ERROR[1002], art_id),
		LatelyTime: time.Now().Unix(),
	}

	res := models.AddPageVisitLog(pvl, art_id, 1)
	a.success(c, "success", res)
	return
}

/**
 * IndexArticleDetail
 * @Description: 通过id获取文章详情
 * @Author: Jazk-Z
 * @receiver a
 * @param c
 */
func (a ArticleController) IndexArticleDetail(c *gin.Context) {
	art_id := c.Query("id")
	valid := validation.Validation{}
	valid.Required(art_id, "文章id丢了")
	if valid.HasErrors() {
		for _, err := range valid.Errors {
			a.error(c, err.Key, "")
			return
		}
	}

	ad, ct := models.ArticleDetailById(art_id)
	if ct != nil {
		a.error(c, "文章不存在", "")
		return
	}
	a.success(c, "success", ad)
	return
}

/**
 * AdminArticleList
 * @Description: 管理后台——文章列表
 * @Author: Jazk-Z
 * @receiver a
 * @param c
 */
func (a ArticleController) AdminArticleList(c *gin.Context) {
	keyword := strings.TrimSpace(c.DefaultQuery("keyword", ""))
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
	offset := (page - 1) * limit

	list, total := models.ArticleListByCat(limit, offset, 0, 0, keyword, 1)

	data := make(map[string]interface{})
	data["data"] = list
	data["total"] = total
	data["url"] = "http://" + c.Request.Host + "/static"
	a.success(c, "success", data)
	return
}
